<!-- HomePage.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title id="title">首页轮播图点击</title>
    <link rel="stylesheet" href="../../css/base.css">
</head>
<body>
<!--<div class="base_navigation_bar base_style_color">
<span>长风认证</span>
</div>-->
<div id='BarAppearance'></div>

<div class="base_navigation_bar base_style_color base_navigation_item_bar" style="color: black">
    <a class="base_left_item" href="#" onclick="backClick()"> <img class="base_back_icon"
                                                                   src="../../image/icon_jiantou.png" alt=""> </a>
    <span id="newsTitle">详情</span>
    <a class="base_right_item" onclick="onShareClick()"> <img class="base_item_icon" src="../../image/icon_lan.png">
    </a>
</div>
<!--<div style="position: fixed;top: 30px;z-index: 999">-->
<!--    <button onclick="test1()">影藏</button>-->
<!--    <button onclick="test2()">显示</button>-->
<!--</div>-->
</body>
<script src="../../script/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/requestTool.js"></script>
<script type="text/javascript" src="../../script/baidubce-sdk.bundle.min.js"></script>
<script type="text/javascript" src="../../script/processing_images.js"></script>
<script type="text/javascript" src="../../script/permission.js"></script>
<script src="../../script/ui.js"></script>
<script type="text/javascript">
    var url = "http://cfnet.org.cn/approve";
    var title = "长风认证";
    var description = "长风网新闻";
    var thumbUrlQQ = "http://ossimg.cfnet.org.cn/img/20170603/112667-1496458368-70714.png";
    var thumbUrlWV = "widget://image/logosmall.png";
    var shareType;
    var thumb;
    var shareimg;
    apiready = function () {
        initHeaderUI();
        if (api.pageParam.ctitle == '轮播图详情页') {
            sendBuriedNotifi('0', '轮播图详情页', '163');
        } else if (api.pageParam.ctitle == '热门资讯详情页') {
            sendBuriedNotifi('0', '热门资讯详情页', '165');
        } else if (api.pageParam.ctitle == '长风之星详情页') {
            sendBuriedNotifi('0', '长风之星详情页', '164');
        } else if (api.pageParam.ctitle == '长风之星新闻详情页') {
            sendBuriedNotifi('0', '长风之星新闻详情页', '167');
        } else if (api.pageParam.ctitle == '空中课堂列表详情页') {
            sendBuriedNotifi('0', '空中课堂列表详情页', '174');
        }
        $('#title').html(api.pageParam.ctitle);
        $('#newsTitle').html(api.pageParam.title);
        url = api.pageParam.shareurl;
//			console.log(url);
        title = api.pageParam.title;
        shareType=api.pageParam.shareType;
        thumb=api.pageParam.thumb;
        shareimg=api.pageParam.shareimg;
        openframe();
        listnbackey();
        eventListener();
    };

    function test1() {
        api.sendEvent({
            name: 'active_control_share',
            extra: {
                isShow: false
            }
        });
    }

    function test2() {
        api.sendEvent({
            name: 'active_open_assets',
            extra: {
                type: "all"
            }
        });
    }

    function eventListener() {
        //todo 设置导航栏隐藏显示
        api.addEventListener({
            name: 'active_control_navigation'
        }, function (ret, err) {
            var y;
            if (ret.value.isShow) {
                y = $('#BarAppearance').height() + $('.base_navigation_bar').height();
                $(".base_navigation_bar").show()
            } else {
                y = $('#BarAppearance').height();
                $(".base_navigation_bar").hide()
            }
            if(ret.value.isLandScape){
                y=0;
                api.setScreenOrientation({
                    orientation: 'landscape_left'
                });
                api.setFullScreen({
                   fullScreen:true
                });
            }
            api.setFrameAttr({
                name: "indexLunDetails",
                rect: {
                    x: 0,
                    y: y,
                    w: 'auto',
                    h: api.winHeight - y
                }
            });
        });
        //todo 打开分享
        api.addEventListener({
            name: 'active_control_share'
        }, function (ret, err) {
            var title = ret.value.title;
            var describe = ret.value.describe;
            var url = ret.value.url;
            api.download({
                url: ret.value.img,
                cache: true,
                allowResume: true
            }, function (ret, err) {
                var imgUrl;
                console.log(JSON.stringify(ret));
                if (ret.state == 1 && ret.savePath.match(".png")) {
                    //下载成功
                    imgUrl = ret.savePath;
                } else {
                    imgUrl = thumbUrlWV;
                }
                share(title, describe, imgUrl, url);
            });
        });
        //todo 打开系统相册
        api.addEventListener({
            name: 'active_open_assets'
        }, function (ret, err) {
            var type = ret.value.type;
            if (type === "camera") {
                checkPermission(['camera'], function (ret) {
                    if (!ret) {
                        api.alert({
                            title: '警告',
                            msg: '相机权限申请失败，该功能无法使用，请到系统设置中打开相机权限'
                        }, function (ret, err) {

                        });
                    } else {
                        //相机
                        api.getPicture({
                            sourceType: 'camera',
                            encodingType: 'png',
                            mediaValue: 'pic',
                            allowEdit: true,
                            quality: 50,
                            saveToPhotoAlbum: false
                        }, function (ret, err) {
                            if (ret !== undefined && ret.data.length > 0) {
                                api.showProgress({
                                    title: '请稍后',
                                    text: '',
                                });
                                sendAssestResult("image", ret.data, 0, ret.data);
                            }
                        });
                    }
                });

            } else if (type === "image" || type === "video" || type === "all") {
                //相册
                var UIAlbumBrowser = api.require('UIAlbumBrowser');
                UIAlbumBrowser.open({
                    max: 1,
                    type: type,
                    styles: {
                        bg: '#fff',
                        mark: {
                            position: 'bottom_left',
                            size: 20
                        },
                        nav: {
                            bg: 'rgba(0,0,0,0.6)',
                            titleColor: '#fff',
                            titleSize: 18,
                            cancelColor: '#fff',
                            cancelSize: 16,
                            finishColor: '#fff',
                            finishSize: 16
                        }
                    },
                    rotation: true
                }, function (ret) {
                    if (ret.eventType === "confirm") {
                        var filePath = ret.list[0].path;//文件路径
                        var fileSize = ret.list[0].size;//文件大小
                        var thumbnail = ret.list[0].thumbPath;//缩略图
                        if (fileSize > 1024 * 1024 * 50) {
                            //视频不能大于50MB
                            api.toast({"msg": "文件不能大于50MB"});
                            return;
                        }
                        if (api.systemType === 'ios') {
                            if (ret.list[0].mediaType == "video") {
                                UIAlbumBrowser.transVideoPath({
                                    path: filePath,
                                    isSave: true
                                }, function (ret, err) {
                                    sendAssestResult("video", ret.savePath, fileSize, thumbnail);
                                });
                            } else {
                                UIAlbumBrowser.transPath({
                                    path: filePath
                                }, function (ret, err) {
                                    sendAssestResult("image", ret.path, fileSize, thumbnail);
                                });
                            }
                        } else {
                            sendAssestResult(ret.list[0].videoPath === undefined ? "image" : "video", filePath, fileSize, thumbnail);
                        }
                    }
                });
            }
        });
        api.addEventListener({
            name:'keyback',
        },function (ret,err) {
            backClick();
        });
        api.addEventListener({
            name:'closeWin',
        },function (ret,err) {
            backClick();
        });
        api.addEventListener({
            name:'setScreenType',
        },function (ret,err) {
            api.setFrameAttr({
               name:'indexLunDetails',
               rect:{
                   x:0,
                   y:0,
                   w:'auto',
                   h:'auto'
               }
            });
            $('#BarAppearance').hide();
        })
    }

    function sendAssestResult(type, filePath, fileSize, thumbnail) {
        //上传文件至百度云
        UploadFile_baidu(type, filePath, function (ret) {
            if (ret) {
                console.log("视频上传成功", thumbnail);
                sendThumbnail(type, ret.url, fileSize, thumbnail);
            } else {
                api.hideProgress();
                api.toast({"msg": "文件上传失败，请稍后再试"});
            }
        });
    }

    //上传封面图
    function sendThumbnail(type, filePath, fileSize, thumbnail) {
        UploadFile_baidu(type, thumbnail, function (ret) {
            api.hideProgress();
            if (ret) {
                api.sendEvent({
                    name: 'active_assets_result',
                    extra: {
                        type: type,
                        filePath: filePath,
                        fileSize: fileSize,
                        thumbnail: ret.url
                    }
                });
            } else {
                api.toast({"msg": "文件上传失败，请稍后再试"});
            }
        });

    }

    function backClick() {
        if (api.pageParam.ctitle == '轮播图详情页') {
            sendBuriedNotifi('1', '轮播图详情页', '163');
        } else if (api.pageParam.ctitle == '热门资讯详情页') {
            sendBuriedNotifi('1', '热门资讯详情页', '165');
        } else if (api.pageParam.ctitle == '热门资讯详情页') {
            sendBuriedNotifi('1', '热门资讯详情页', '164');
        } else if (api.pageParam.ctitle == '长风之星新闻详情页') {
            sendBuriedNotifi('1', '长风之星新闻详情页', '167');
        } else if (api.pageParam.ctitle == '空中课堂列表详情页') {
            sendBuriedNotifi('1', '空中课堂列表详情页', '174');
        }
        api.setFullScreen({
            fullScreen:false
        });
        api.setScreenOrientation({
            orientation:'portrait_up'
        });
        api.closeWin();
    }

    function sendBuriedNotifi(busionessid, pagetype, pagetypeid) {
        var userinfo = {
            uid: $api.getStorage('cfnetppuid'),
            jid: $api.getStorage('cfnetppjxid'),
            uname: $api.getStorage('cfnetppusername')
        }
        if (pagetypeid == '174') {
            userinfo['message'] = '直播课程' + "\"" + api.pageParam.title + "\"";
        }
        var data = {
            busionessid: busionessid,
            pagetype: pagetype,
            pagetypeid: pagetypeid,
            sourceurl: api.pageParam.sourceurl,
            sourcetitle: api.pageParam.sourcetitle,
            accesspageurl: window.location.pathname,
            accesspagetitle: document.title,
            userinfo: userinfo
        }
        SendBehaviorTrajectoriesNotifi_Study(data);
    }

    function listnbackey() {
        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            //coding...
            backClick();
        });
    }

    function openframe() {
//		alert(api.pageParam.url);
//		console.log(api.pageParam.url);
        var y = $('#BarAppearance').height() + $('.base_navigation_bar').height();
        var h = api.winHeight - y;
        api.openFrame({
            name: 'indexLunDetails',
            url: api.pageParam.url,
            rect: {
                x: 0,
                y: y,
                w: 'auto',
                h: h
            },
            progress: {//设置进度条类型和样式
                type: 'page',
                color: '#18b4ed'
            },
            reload: true,
            allowEdit: true,
            useWKWebView: true,
            bgColor: '#ffffff',
            scaleEnabled: true
        });
    }

    //分享
    function onShareClick() {
        share(title, description, thumbUrlQQ, url)
    }

    function share(title, description, img, url) {
        var message;
        var dialogBox = api.require('dialogBox');
        dialogBox.actionMenu({
            tapClose: true,
            rect: {
                h: 208
            },
            texts: {
                cancel: '取消'
            },
            items: [{
                text: '分享到微信',
                icon: 'widget://image/icon_weixin.png'
            }, {
                text: '分享到朋友圈',
                icon: 'widget://image/icon_pengyouquan.png'
            }
                // , {
                // 	text : '分享到手机QQ',
                // 	icon : 'widget://image/icon_qq.png'
                // }, {
                // 	text : '分享到QQ空间',
                // 	icon : 'widget://image/icon_kongjian.png'
                // }
            ],
            styles: {
                bg: '#F0F0F0',
                column: 4,
                itemText: {
                    color: '#000',
                    size: 12,
                    marginT: 8
                },
                itemIcon: {
                    size: 60
                },
                cancel: {
                    // bg:'widget://image/icno_share_buttom.png',
                    color: '#535353',
                    h: 38,
                    size: 14
                }
            }
        }, function (ret) {
            var eventType = ret.eventType;
            //判断类型
            var index = ret.index;
            if (eventType == 'cancel') {//关闭
                dialogBox.close({
                    dialogName: 'actionMenu'
                });
                return;
            }
            if (index == 2 || index == 3) {
                var qq = api.require('qq');
                qq.installed(function (ret, err) {
                    if (ret.status) {
                        if (index == 2) {//qq
                            qq.shareNews({
                                url: url,
                                type: 'QFriend',
                                title: title,
                                description: description,
                                imgUrl: img
                            }, function (ret, err) {
                                dialogBox.close({
                                    dialogName: 'actionMenu'
                                });
                                if (ret.status) {
                                    message = title + "分享至QQ";
                                    sendBuriedPointNotifi(message);
                                    api.toast({
                                        msg: '分享成功QQ'
                                    });
                                } else {
                                    if (err.code == -4) {
                                        api.toast({
                                            msg: '用户取消分享'
                                        });
                                    } else if (err.code == 10000) {
                                        api.toast({
                                            msg: 'QQ空间分享不支持文本类型分享'
                                        });
                                    } else if (err.code == 10001) {
                                        api.toast({
                                            msg: 'QQ空间分享不支持图片类型分享'
                                        });
                                    } else if (err.code == 10009) {
                                        api.toast({
                                            msg: '当前设备未安装QQ客户端'
                                        });
                                    } else {
                                        api.toast({
                                            msg: '分享失败QQ'
                                        });
                                    }
                                }
                            });
                        } else if (index == 3) {//qq空间
                            qq.shareNews({
                                url: url,
                                type: 'QZone',
                                title: title,
                                description: description,
                                imgUrl: img
                            }, function (ret, err) {
                                dialogBox.close({
                                    dialogName: 'dialogBox'
                                });
                                if (ret.status) {
                                    message = title + "分享至QQ空间";
                                    sendBuriedPointNotifi(message)
                                    api.toast({
                                        msg: '分享成功QQ空间'
                                    });
                                } else {
                                    if (err.code == -4) {
                                        api.toast({
                                            msg: '用户取消分享'
                                        });
                                    } else if (err.code == 10000) {
                                        api.toast({
                                            msg: 'QQ空间分享不支持文本类型分享'
                                        });
                                    } else if (err.code == 10001) {
                                        api.toast({
                                            msg: 'QQ空间分享不支持图片类型分享'
                                        });
                                    } else if (err.code == 10009) {
                                        api.toast({
                                            msg: '当前设备未安装QQ客户端'
                                        });
                                    } else {
                                        api.toast({
                                            msg: '分享失败QQ空间'
                                        });
                                    }
                                }
                            });
                        }
                    } else {
                        dialogBox.close({
                            dialogName: 'actionMenu'
                        });
                        alert("没有安装QQ");
                        // $("#passFailCount").show();
                        //  $("#title_text").html('没有安装QQ');
                    }
                });
            }
            if (index == 0 || index == 1) {
                var wx = api.require('wx');
                wx.isInstalled(function (ret, err) {
                    if (ret.installed) {
                        if (index == 0) {//微信
                            if(shareType=="image"){
                                shareedImg('session');
                                return;
                            }
                            wx.shareWebpage({
                                apiKey: '',
                                scene: 'session',
                                title: title,
                                description: description,
                                thumb: img,
                                contentUrl: url
                            }, function (ret, err) {
                                dialogBox.close({
                                    dialogName: 'actionMenu'
                                });
                                if (ret.status) {
                                    message = title + "分享至微信好友";
                                    sendBuriedPointNotifi(message);
                                    api.toast({
                                        msg: '分享成功微信好友'
                                    });
                                } else {
                                    api.toast({
                                        msg: '分享失败微信好友'
                                    });
                                }
                            });
                        } else if (index == 1) {//朋友圈
                            if(shareType=="image"){
                                shareedImg('timeline');
                                return;
                            }
                            wx.shareWebpage({
                                apiKey: '',
                                scene: 'timeline',
                                title: title,
                                description: description,
                                thumb: img,
                                contentUrl: url
                            }, function (ret, err) {
                                dialogBox.close({
                                    dialogName: 'actionMenu'
                                });
                                if (ret.status) {
                                    message = title + "分享至朋友圈";
                                    sendBuriedPointNotifi(message);
                                    api.toast({
                                        msg: '分享成功朋友圈'
                                    });
                                } else {
                                    api.toast({
                                        msg: '分享失败朋友圈'
                                    });
                                }
                            });
                        }
                    } else {
                        dialogBox.close({
                            dialogName: 'actionMenu'
                        });
                        //			   $("#passFailCount").show();
                        //				       $("#title_text").html('没有安装微信');
                    }
                });
            }
        });
    }
    function shareedImg(scene){
        showProgress();
        var date=Date.now();
        api.download({
            url: thumb,
            savePath: 'fs://thumb_'+date+'.png',
            report: true,
            cache: false,
            allowResume: true
        }, function(ret, err) {
            if (ret.state == 1) {
                api.download({
                    url: shareimg,
                    savePath: 'fs://sharedImg_'+date+'.png',
                    report: true,
                    cache: false,
                    allowResume: true
                }, function(ret, err) {
                    hideProgress();
                    if (ret.state == 1) {
                        var wx = api.require('wx');
                        wx.shareImage({
                            apiKey: '',
                            scene: scene,
                            thumb: 'fs://thumb_'+date+'.png',
                            contentUrl:  'fs://sharedImg_'+date+'.png',
                        }, function(ret, err) {
                            if (ret.status) {
                                alert('分享成功');
                            } else {
                                alert(err.code);
                            }
                        });
                    } else {
                        hideProgress();
                        api.toast({
                            msg: '分享失败'
                        });
                    }
                });
            } else {
                hideProgress();
                api.toast({
                    msg: '分享失败'
                });
            }
        });
    }
    //发送埋点通知
    function sendBuriedPointNotifi(message) {
        var param = new Object();
        var data = new Object();
        param["message"] = message;
        param["uid"] = $api.getStorage('cfnetppuid');
        param["jid"] = $api.getStorage('cfnetppjxid');
        if (api.pageParam.Newstype == "HotNews") {
            SendBusinessOperationsLogNotifi('新闻页', '165', param, param, '', data, '热门资讯分享新闻', '165-01')
        } else if (api.pageParam.Newstype == "cfStar") {
            SendBusinessOperationsLogNotifi('新闻页', '167', param, param, '', data, '长风之星分享新闻', '167-01')
        } else if (api.pageParam.Newstype == "lunbo") {
            SendBusinessOperationsLogNotifi('新闻页', '167', param, param, '', data, '首页轮播图分享新闻', '167-01')
        } else {
            SendBusinessOperationsLogNotifi('新闻页', '150', param, param, '', data, '认证分享新闻', '150-01')
        }
    }
</script>
</html>